// PartJoin.cpp: implementation of the PartJoin class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PartJoin.h"
#include "PHSFactory.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

PartJoin::PartJoin(Part* pPrev, Part* pCur)
{
	m_pPrev = pPrev;
	m_pCur = pCur;

	ReadJoin();
}

PartJoin::~PartJoin()
{
	for(int i = 0; i < m_theAttachs.GetSize(); i++)
	{
		delete m_theAttachs.GetAt(i);
	}
	m_theAttachs.RemoveAll();
}

void PartJoin::ReadJoin()
{
	GetCurDatabase().ReadJoin(this);

	if(m_pPrev == NULL)
		return;

	for(int i = 0; i < m_theAttachs.GetSize(); i++)
	{
		m_theAttachs.GetAt(i)->SetPartNum(m_pPrev->GetPartNum()*m_pPrev->GetParalleledNum());
	}
}

BOOL PartJoin::Save()
{
	for(int i = 0; i < m_theAttachs.GetSize(); i++)
	{
	}
	return TRUE;
}

BOOL PartJoin::SetParam(CWnd* pParentWnd, BOOL bIsVisible)
{
	return TRUE;
}

BOOL PartJoin::Create()
{
	return TRUE;
}

CString PartJoin::GetObjectName()
{
	return CString(_T("Join"));
}

void PartJoin::SetState(INT nState)
{
	ModelObject::SetState(nState);
	
	for(int i = 0; i < m_theAttachs.GetSize(); i++)
	{
		m_theAttachs.GetAt(i)->SetState(nState);
	}	
}

Part* PartJoin::GetFirstAttach()
{
	if(m_theAttachs.GetSize() <= 0)
		return NULL;

	return m_theAttachs.GetAt(0);
}

Part* PartJoin::GetLastAttach()
{
	if(m_theAttachs.GetSize() <= 0)
		return NULL;
	
	return m_theAttachs.GetAt(m_theAttachs.GetSize() - 1);
}

Part* PartJoin::GetNextAttach(Part* pPart)
{
	for(int i = 0; i < m_theAttachs.GetSize(); i++)
	{
		if(pPart == m_theAttachs.GetAt(i))
		{
			if(i == m_theAttachs.GetSize() - 1)
				return NULL;
			return m_theAttachs.GetAt(i + 1);
		}
	}
	return NULL;
}

Part* PartJoin::GetPrevAttach(Part* pPart)
{
	for(int i = 0; i < m_theAttachs.GetSize(); i++)
	{
		if(pPart == m_theAttachs.GetAt(i))
		{
			if(i == 0)
				return NULL;
			return m_theAttachs.GetAt(i - 1);
		}
	}
	return NULL;
}
